import {Component, OnInit} from '@angular/core';
import {GooGoods, GooGoodsCondition} from '../../goo-goods.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifyGooGoodsComponent} from '../modify-goo-goods.component';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {GooBrandCondition} from '../../../goo-brand/goo-brand.type';
import {GooTypeLevel1Condition} from '../../../goo-type-level1/goo-type-level1.type';
import {GooTypeLevel2Condition} from '../../../goo-type-level2/goo-type-level2.type';
import {FroalaEditor} from '../../../../../frame/global/froala-editor';

@Component({
    selector: 'app-main-modify-goo-goods',
    templateUrl: './main-modify-goo-goods.component.html',
    styleUrls: ['./main-modify-goo-goods.component.css']
})
export class MainModifyGooGoodsComponent implements OnInit {

    froalaEditorOptions = Object.assign(FroalaEditor.options, {});

    errRes = new ErrRes();

    domain: GooGoods = new GooGoods();

    extend = {
        domain: {
            serLevel1: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '一级分类类型',
                change: (event) => {
                    console.log(event);
                }
            },
            serLevel2: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '二级分类类型',
                change: (event) => {
                    console.log(event);
                }
            },
            serBrand: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '品牌',
                change: (event) => {
                    console.log(event);
                }
            },
            default: () => {
                this.domain.serAudit = 'WAIT_SUBMIT';
                this.domain.serUsing = 'ENABLE';
            },
            serAudit: () => {
                return 'serAudit';
            },
            serGoodsStartTime: {
                icon: fa.faCalendar,
                nickname: '打折开始时间',
                value: () => {
                    // return `${this.condition.createTimeOUTSET} - ${this.condition.createTimeFINISH}`;
                    return this.domain.serGoodsStartTime;
                },
                focus: (event) => {
                    this.domain.serGoodsStartTime = event.target.value;
                },
                blur: (event) => {
                }
            },
            serGoodsEndTime: {
                icon: fa.faCalendar,
                nickname: '打折结束时间',
                value: () => {
                    // return `${this.condition.createTimeOUTSET} - ${this.condition.createTimeFINISH}`;
                    return this.domain.serGoodsEndTime;
                },
                focus: (event) => {
                    this.domain.serGoodsEndTime = event.target.value;
                },
                blur: (event) => {
                }
            },
            fileSerGoodsP1: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileSerGoodsP1.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileSerGoodsP1.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsP1 = this.extend.domain.fileSerGoodsP1.oss.fileUrl;
                        } else {
                            this.extend.domain.fileSerGoodsP1.oss.fileUrl = '';
                            this.domain.serGoodsP1 = this.extend.domain.fileSerGoodsP1.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            fileSerGoodsP2: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileSerGoodsP2.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileSerGoodsP2.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsP2 = this.extend.domain.fileSerGoodsP2.oss.fileUrl;
                        } else {
                            this.extend.domain.fileSerGoodsP2.oss.fileUrl = '';
                            this.domain.serGoodsP2 = this.extend.domain.fileSerGoodsP2.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            fileSerGoodsP3: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.fileSerGoodsP3.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.fileSerGoodsP3.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsP3 = this.extend.domain.fileSerGoodsP3.oss.fileUrl;
                        } else {
                            this.extend.domain.fileSerGoodsP3.oss.fileUrl = '';
                            this.domain.serGoodsP3 = this.extend.domain.fileSerGoodsP3.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
            /*            files: {
                            fileUrls: [],
                            oss: Services.aliOSS.simple(),
                            change: (event) => {
                                console.log(event.target.value); // file name
                                this.extend.domain.files.oss.uploadFile((response, status, headers) => {
                                    if (status === 200) {
                                        this.extend.domain.files.fileUrls.push(JSON.parse(response).detail.fileUrl);
                                        this.extend.domain.files.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                                        this.domain.serGoodsBanner = this.extend.domain.files.fileUrls.toString();
                                    } else {
                                        this.extend.domain.files.oss.fileUrl = '';
                                        this.domain.serGoodsBanner = this.extend.domain.files.fileUrls.toString();
                                        Zero3Alert.req('fail');
                                    }
                                });
                            }
                        },*/
            files: {
                fileUrls: [],
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.files.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.files.fileUrls.push(JSON.parse(response).detail.fileUrl);
                            this.extend.domain.files.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serGoodsBanner = this.extend.domain.files.fileUrls.toString();
                        } else {
                            this.extend.domain.files.oss.fileUrl = '';
                            this.domain.serGoodsBanner = this.extend.domain.files.fileUrls.toString();
                            Zero3Alert.req('fail');
                        }
                    });
                },
                del: {
                    click: (event, fileUrl) => {
                        const urlIndex = this.extend.domain.files.fileUrls.findIndex(item => item === fileUrl);
                        this.extend.domain.files.fileUrls.splice(urlIndex, 1);
                        this.domain.serGoodsBanner = this.extend.domain.files.fileUrls.toString();
                    }
                }
            },
        },
    };
    func = {
        loadDomain: (uuid: string) => {
            const condition: GooGoodsCondition = new GooGoodsCondition();
            condition.uuid = uuid;
            Services.gooGoods.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                    if (!isNullOrUndefined(this.domain.serGoodsBanner)) {
                        this.extend.domain.files.fileUrls = this.domain.serGoodsBanner.split(',');
                    }
                }
            });
        },
        back: {
            req: (event) => {
                this.router.navigate([UrlRou.gooGoods.list]);
            }
        },
        update: {
            req: (event) => {
                Services.gooGoods.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.gooGoods.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public modifyGooGoods: ModifyGooGoodsComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifyGooGoods.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
        Services.gooBrand.list(new GooBrandCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.serBrand.items = [];
                item.page.content.forEach(brand => {
                    this.extend.domain.serBrand.items.push({id: brand.uuid, name: brand.serBarndName});
                });
            }
        });
        Services.gooTypeLevel1.list(new GooTypeLevel1Condition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.serLevel1.items = [];
                item.page.content.forEach(level1 => {
                    this.extend.domain.serLevel1.items.push({id: level1.uuid, name: level1.serTypeName});
                });
            }
        });
        Services.gooTypeLevel2.list(new GooTypeLevel2Condition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.serLevel2.items = [];
                item.page.content.forEach(level2 => {
                    this.extend.domain.serLevel2.items.push({id: level2.uuid, name: level2.serTypeName});
                });
            }
        });
    }
}
